/* $Id: mywintab.c,v 1.2 2000/02/01 05:40:21 rekimoto Exp $ */

#include <stdio.h>
#include <string.h>
#include <windows.h>
#include <stdlib.h>

/* wintab */
#include "INCLUDE/WINTAB.H"
#ifdef USE_X_LIB
#include <wintabx.h>
#endif
#define PACKETDATA	(PK_X | PK_Y | PK_BUTTONS | PK_ORIENTATION | PK_NORMAL_PRESSURE | PK_CURSOR)
#define PACKETMODE	0
#include "INCLUDE/Pktdef.H"

#include "mywintab.h"

static HCTX ctx;

static HCTX TabletInit(HWND hWnd, int val[]) {
	LOGCONTEXT lcMine;
	AXIS            TabletX, TabletY; /* The maximum tablet size */

	/* get default region */
	WTInfoA(WTI_DEFCONTEXT, 0, &lcMine);

	/* modify the digitizing region */
	strcpy(lcMine.lcName, "Rule Digitizing");
	lcMine.lcPktData = PACKETDATA;
	lcMine.lcPktMode = PACKETMODE;
	lcMine.lcMoveMask = 0;
	lcMine.lcBtnUpMask = lcMine.lcBtnDnMask;

    /* Get the max coords */
	WTInfoA(WTI_DEVICES,DVC_X,&TabletX);
	WTInfoA(WTI_DEVICES,DVC_Y,&TabletY);

	val[0] = TabletX.axMin;
	val[1] = TabletX.axMax;
	val[2] = TabletY.axMin;
	val[3] = TabletY.axMax;

	//lcMine.lcInOrgX = 0;
	//lcMine.lcInOrgY = 0;
	//lcMine.lcInExtX = TabletX.axMax;
	//lcMine.lcInExtY = TabletY.axMax;

 //   /* output the data in screen coords */
	//lcMine.lcOutOrgX = lcMine.lcOutOrgY = 0;
	//lcMine.lcOutExtX = GetSystemMetrics(SM_CXSCREEN);
 //   /* move origin to upper left */
	//lcMine.lcOutExtY = -GetSystemMetrics(SM_CYSCREEN);

	/* open the region */
	return WTOpen(hWnd, &lcMine, TRUE);
}

int mwOpen(int val[]) {
	HWND hWnd;
	if (ctx != NULL) return -2;
	hWnd = GetDesktopWindow();
	if (hWnd == NULL) return -3;
	ctx = TabletInit(hWnd, val);
	if (ctx == NULL) return -4;
 	return 1;
}

int mwClose() {
	if (ctx == NULL) return 1;
	return WTClose(ctx);
}

int mwGetPacket(int val[]) {
	PACKET p;
	
	if (ctx == NULL) return -1;
	if (WTPacketsGet(ctx, 1, &p) > 0) { /* received a packet */
		val[0] = p.pkX;
		val[1] = p.pkY;
		val[2] = p.pkButtons;	
		val[3] = p.pkOrientation.orAzimuth;
		val[4] = p.pkOrientation.orAltitude;
		// added 1.29.2000
		val[5] = p.pkNormalPressure;
		val[6] = p.pkCursor;
		return 1;
	}
	return 0; /* no packet has arrived */
}


BOOL mwOverlap() {
	return WTOverlap(ctx, TRUE);
}

BOOL mwUnderlap() {
	return WTOverlap(ctx, FALSE);
}